// var o;
// exports.EmailData = void 0;
// var $storageClass = require("../../resources/scripts/AL_Framework_Storage_Class");
// var AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
// var AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
// var c = {
//     email_record: []
// };
// var l = (function (e) {
//     function t() {
//         return (null !== e && e.apply(this, arguments)) || this;
//     }
//     __extends(t, e);
//     t.getInstance = function () {
//         return AL_APP_SDK_MANAGER.localManager.huoqu_cunchu_lei(t);
//     };
//     t.prototype.zhixing_chushihua_chuli = function () {
//         this.chushihua_cunchu_shuju(c);
//         this.chushihua_yuancheng_key(Object.keys(c));
//     };
//     t.prototype.huoqu_suoyou_youjian = function () {
//         return this.email_record;
//     };
//     t.prototype.jiancha_youjian_shifou_youxiao = function (e) {
//         var t = AL_GAME_HUA_Common.Common.huoqu_dangqian_shijian();
//         if (e.validTime) {
//             var n = new Date(e.createTime);
//             n.setHours(0, 0, 0, 0);
//             var o = n.getTime() + 864e5 * (e.validTime + 1);
//             return {
//                 valid: t < o,
//                 time: o - t
//             };
//         }
//         return {
//             valid: !0,
//             time: -1
//         };
//     };
//     t.prototype.huoqu_weidu_youjian = function () {
//         var e = [];
//         for (var t = 0; t < this.email_record.length; t++) {
//             var n = this.email_record[t];
//             if (this.jiancha_youjian_shifou_youxiao(n).valid) {
//                 if (!n.view) {
//                     e.push(n);
//                 }
//             }
//         }
//         return e;
//     };
//     t.prototype.huoqu_weilingqu_youjian = function () {
//         var e = [];
//         for (var t = 0; t < this.email_record.length; t++) {
//             var n = this.email_record[t];
//             if (this.jiancha_youjian_shifou_youxiao(n).valid) {
//                 if (!n.get) {
//                     e.push(n);
//                 }
//             }
//         }
//         return e;
//     };
//     t.prototype.tianjia_youjian = function (e) {
//         e.view = !1;
//         e.get = !1;
//         e.deleted = !1;
//         this.email_record.push(e);
//         this.shezhi_cunchu_shuju("email_record", this.email_record);
//         AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.REFRESH_EMAIL_LIST, this.email_record);
//     };
//     t.prototype.baocun_youjian = function (e) {
//         this.email_record = e;
//         this.shezhi_cunchu_shuju("email_record", this.email_record);
//         AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.REFRESH_EMAIL_LIST, this.email_record);
//     };
//     t.prototype.genggai_youjian = function (e) {
//         for (var t = 0; t < this.email_record.length; t++) {
//             var n = this.email_record[t];
//             if (n.id == e.id) {
//                 if (e.view) {
//                     n.view = e.view;
//                 }
//                 if (e.get) {
//                     n.get = e.get;
//                 }
//                 if (e.deleted) {
//                     n.deleted = e.deleted;
//                 }
//                 this.shezhi_cunchu_shuju("email_record", this.email_record);
//                 AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.REFRESH_EMAIL_LIST, this.email_record);
//                 break;
//             }
//         }
//     };
//     t.bundle = AL_GAME_HUA_Define.Define.GAME_NAME;
//     t.key = "_em";
//     return t;
// })($storageClass.AL_Framework_Storage_Class);
// exports.EmailData = l;


let instance;
exports.EmailData = void 0;
const AL_Framework_Storage_Class = require("../../resources/scripts/AL_Framework_Storage_Class");
const Common = require("./AL_GAME_HUA_Common");
const Define = require("./AL_GAME_HUA_Define");

/** 
 * 邮件管理类，用于处理邮件相关的存储和业务逻辑
 * @extends AL_Framework_Storage_Class
 */
class EmailHandler extends AL_Framework_Storage_Class.AL_Framework_Storage_Class {

    bundle = Define.Define.GAME_NAME;
    key = "_em";
    constructor() {
        super();
        this.emailRecord = [];
    }

    /**
     * 获取类的单例实例
     * @returns {EmailHandler} 实例
     */
    static getInstance() {
        if (!instance) {
            instance = new EmailHandler();
            instance.zhixing_chushihua_chuli();
        }
        return instance;
    }

    /**
     * 初始化类
     */
    zhixing_chushihua_chuli() {
        this.chushihua_cunchu_shuju(this.emailRecord);
        this.chushihua_yuancheng_key(Object.keys({ email_record: [] }));
    }

    /**
     * 获取所有邮件
     * @returns {Array} 邮件数组
     */
    huoqu_suoyou_youjian() {
        return this.emailRecord;
    }

    /**
     * 检查邮件是否有效
     * @param {Object} email - 邮件对象
     * @returns {Object} {valid: Boolean, time: Number}
     */
    jiancha_youjian_shifou_youxiao(email) {
        const currentTime = Common.Common.huoqu_dangqian_shijian();
        if (email.validTime) {
            const createDate = new Date(email.createTime);
            createDate.setHours(0, 0, 0, 0);
            const expirationTime = createDate.getTime() + 864e5 * (email.validTime + 1);
            return {
                valid: currentTime < expirationTime,
                time: expirationTime - currentTime
            };
        }
        return {
            valid: true,
            time: -1
        };
    }

    /**
     * 获取未读邮件
     * @returns {Array} 未读邮件数组
     */
    huoqu_weidu_youjian() {
        return this.emailRecord.filter(email => this.jiancha_youjian_shifou_youxiao(email).valid && !email.view);
    }

    /**
     * 获取未领取的邮件
     * @returns {Array} 未领取邮件数组
     */
    huoqu_weilingqu_youjian() {
        return this.emailRecord.filter(email => this.jiancha_youjian_shifou_youxiao(email).valid && !email.get);
    }

    /**
     * 添加邮件
     * @param {Object} email - 新邮件对象
     */
    tianjia_youjian(email) {
        email.view = false;
        email.get = false;
        email.deleted = false;
        this.emailRecord.push(email);
        this.shezhi_cunchu_shuju("email_record", this.emailRecord);
        AL_APP_SDK_MANAGER.emit(Define.GameEvent.REFRESH_EMAIL_LIST, this.emailRecord);
    }

    /**
     * 保存邮件
     * @param {Array} emails - 邮件数组
     */
    baocun_youjian(emails) {
        this.emailRecord = emails;
        this.shezhi_cunchu_shuju("email_record", this.emailRecord);
        AL_APP_SDK_MANAGER.emit(Define.GameEvent.REFRESH_EMAIL_LIST, this.emailRecord);
    }

    /**
     * 更改邮件
     * @param {Object} email - 修改后的邮件对象
     */
    genggai_youjian(email) {
        for (let i = 0; i < this.emailRecord.length; i++) {
            let emailRecord = this.emailRecord[i];
            if (emailRecord.id === email.id) {
                if (email.view !== undefined) emailRecord.view = email.view;
                if (email.get !== undefined) emailRecord.get = email.get;
                if (email.deleted !== undefined) emailRecord.deleted = email.deleted;
                this.shezhi_cunchu_shuju("email_record", this.emailRecord);
                AL_APP_SDK_MANAGER.emit(Define.GameEvent.REFRESH_EMAIL_LIST, this.emailRecord);
                break;
            }
        }
    }
}

exports.EmailData = EmailHandler;